home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / bnklysrc / b_frproc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-05  |  19.4 KB  |  628 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                          */
  3. /*                                                                          */
  4. /*      ------------         Bit-Bucket Software <no-Inc>                   */
  5. /*      \ 10001101 /         Writers and Distributors of                    */
  6. /*       \ 011110 /          No-Cost<no-tm> Software.                       */
  7. /*        \ 1011 /                                                          */
  8. /*         ------                                                           */
  9. /*                                                                          */
  10. /*  Copyright (C) 1987, 1988, 1989 by Robert Hartman and Vincent Perriello  */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                   BinkleyTerm File Request Processor                     */
  14. /*                                                                          */
  15. /*                                                                          */
  16. /*    For complete  details  of the licensing restrictions, please refer    */
  17. /*    to the License  agreement,  which  is published in its entirety in    */
  18. /*    the MAKEFILE and BT.C, and also contained in the file LICENSE.210.    */
  19. /*                                                                          */
  20. /*    USE  OF THIS FILE IS SUBJECT TO THE  RESTRICTIONS CONTAINED IN THE    */
  21. /*    BINKLEYTERM  LICENSING  AGREEMENT.  IF YOU DO NOT FIND THE TEXT OF    */
  22. /*    THIS  AGREEMENT IN ANY OF THE  AFOREMENTIONED FILES,  OR IF YOU DO    */
  23. /*    NOT HAVE THESE FILES,  YOU SHOULD  IMMEDIATELY CONTACT THE AUTHORS    */
  24. /*    AT THE  ADDRESSES LISTED BELOW.  IN NO EVENT SHOULD YOU PROCEED TO    */
  25. /*    USE   THIS  FILE  WITHOUT  HAVING   ACCEPTED  THE  TERMS  OF   THE    */
  26. /*    BINKLEYTERM  LICENSING AGREEMENT,  OR SUCH OTHER  AGREEMENT AS YOU    */
  27. /*    ARE ABLE TO REACH WITH THE AUTHORS.                                   */
  28. /*                                                                          */
  29. /*                                                                          */
  30. /*    The Authors can be reached at the following addresses:                */
  31. /*                                                                          */
  32. /*    Robert C. Hartman                      Vincent E. Perriello           */
  33. /*    Spark Software                         VEP Software                   */
  34. /*    427-3 Amherst Street                   111 Carroll Street             */
  35. /*    CS2032, Suite 232                      Naugatuck, CT 06770            */
  36. /*    Nashua, NH 03061                                                      */
  37. /*                                                                          */
  38. /*    FidoNet 1:132/101                      FidoNet 1:141/491              */
  39. /*    Data    (603) 888-8179                 Data    (203) 729-7569         */
  40. /*                                                                          */
  41. /*    Please feel free to contact us at any time to share your comments     */
  42. /*    about our software and/or licensing policies.                         */
  43. /*                                                                          */
  44. /*                                                                          */
  45. /*  This module is based largely on a similar module in OPUS-CBCS V1.03b.   */
  46. /*  The original work is (C) Copyright 1987, Wynn Wagner III. The original  */
  47. /*  author has graciously allowed us to use his code in this work.          */
  48. /*                                                                          */
  49. /*--------------------------------------------------------------------------*/
  50.  
  51. #include <signal.h>
  52. #include <ctype.h>
  53. #include <conio.h>
  54. #include <string.h>
  55. #include <stdlib.h>
  56. #include <time.h>
  57.  
  58. #ifdef __TURBOC__
  59. #include <mem.h>
  60. #else
  61. #include <memory.h>
  62. #endif
  63.  
  64. #define WAZOO_SECTION
  65. #include "com.h"
  66. #include "xfer.h"
  67. #include "zmodem.h"
  68. #include "keybd.h"
  69. #include "sbuf.h"
  70. #include "sched.h"
  71. #include "externs.h"
  72. #include "prototyp.h"
  73. #include "find.h"   /*PLF Fri  05-05-1989  23:54:23 */
  74.  
  75.  
  76. static char *their_pwd;                         /* Password in REQ file */
  77. static char required_pwd[10];                   /* Password in OK file  */
  78.  
  79. static int prep_match (char *, char *);
  80. static int match (char *, char *);
  81. static void run_prog (char *);
  82. static int check_password (void);
  83. static freq_abort (int);
  84.  
  85. static int prep_match (template, buffer)
  86. char *template;
  87. char *buffer;
  88. {
  89.    register int i, delim;
  90.    register char *sptr;
  91.    int start;
  92.  
  93.    memset (buffer, 0, 11);
  94.  
  95.    i = strlen (template);
  96.    sptr = template;
  97.  
  98.  
  99.    for (start = i = 0; sptr[i]; i++)
  100.       if ((sptr[i] == '\\') || (sptr[i] == ':'))
  101.          start = i + 1;
  102.  
  103.    if (start)
  104.       sptr += start;
  105.    delim = 8;                                    /* last column for ? */
  106.  
  107.    strupr (sptr);
  108.  
  109.    for (i = 0; *sptr && i < 12; sptr++)
  110.       switch (*sptr)
  111.          {
  112.          case '.':
  113.             if (i > 8)
  114.                return (-1);
  115.             while (i < 8)
  116.                {
  117.                buffer[i++] = ' ';
  118.                }
  119.             buffer[i++] = *sptr;
  120.             delim = 12;
  121.             break;
  122.  
  123.          case '*':
  124.             while (i < delim)
  125.                {
  126.                buffer[i++] = '?';
  127.                }
  128.             break;
  129.  
  130.          default:
  131.             buffer[i++] = *sptr;
  132.             break;
  133.  
  134.          }                                       /* switch */
  135.  
  136.    while (i < 12)
  137.       {
  138.       if (i == 8)
  139.          buffer[i++] = '.';
  140.       else buffer[i++] = ' ';
  141.       }
  142.  
  143.    buffer[i] = '\0';
  144.  
  145.    return 0;
  146. }
  147.  
  148. static int match (s1, s2)
  149. char *s1, *s2;
  150. {
  151.    register char *i, *j;
  152.  
  153.    i = s1;
  154.    j = s2;
  155.  
  156.    while (*i)
  157.       {
  158.       if ((*j != '?') && (*i != *j))
  159.          {
  160.          return 1;
  161.          }
  162.       i++;
  163.       j++;
  164.       }
  165.  
  166.    return 0;
  167. }
  168.  
  169. /*--------------------------------------------------------------------------*/
  170. /* Process file requests from the remote system. The filespec requested is  */
  171. /* turned into a local filespec if possible, then transferred via the       */
  172. /* caller-supplied routine.                                                 */
  173. /*--------------------------------------------------------------------------*/
  174.  
  175. int n_frproc (request, nfiles, callback)
  176. char *request;
  177. int nfiles;
  178. int (*callback)(char *);
  179. {
  180.    register int i;
  181.    register int j = 0;
  182.    static char s[80];
  183.    static char s1[80];
  184.    static char s2[80];
  185.  
  186.    FILE *approved;
  187.    FSCAN *dh;   /*PLF Fri  05-05-1989  23:48:51 */
  188.    struct stat st;
  189.    char *sptr;
  190.  
  191.    char *after_pwd;
  192.    long updreq = 0L;
  193.    char updtype = 0;
  194.    int saved_nfiles;
  195.  
  196.    char our_wildcard[15];
  197.    char their_wildcard[15];
  198.    int mfunc;
  199.    int magic_state = 0;
  200.  
  201.    int failure_reason = 1;                      /* 1 = not available */
  202.                                                 /* 2 = no update     */
  203.                                                 /* 3 = bad password  */
  204.    approved = NULL;
  205.    their_pwd = NULL;
  206.    after_pwd = NULL;
  207.    strcpy (s1, request);
  208.  
  209.    /*--------------------------------------------------------------------*/
  210.    /* Fix up the file name                                               */
  211.    /*--------------------------------------------------------------------*/
  212.    for (i = 0; request[i]; i++)
  213.       {
  214.       if (request[i] <= ' ')
  215.          {
  216.          request[i++] = '\0';
  217.          j = i;
  218.          break;
  219.          }
  220.       }
  221.    
  222.    if (j)
  223.       {
  224.       /* If we have a '!', find the end of the password, point j
  225.          past it, then truncate and fold if necessary. This leaves
  226.          j properly aligned for other fields.
  227.        */
  228.  
  229.       if (request[j] == '!')
  230.          {
  231.          their_pwd = request + (++j);
  232.          for (; request[j]; j++)
  233.             {
  234.             if (request[j] <= ' ')
  235.                {
  236.                request[j++] = '\0';
  237.                break;
  238.                }
  239.             }
  240.  
  241.          if (strlen (their_pwd) > 6)
  242.             their_pwd[6] = '\0';
  243.  
  244.          fancy_str (their_pwd);
  245.          }
  246.  
  247.       /* Test for update/backdate request */
  248.  
  249.       if (request[j] == '+' || request[j] == '-')
  250.          {
  251.          updtype = request[j++];
  252.          updreq = atol (&request[j]);
  253.          }
  254.       }      
  255.  
  256.    if (!request[0])                             /* Still any filename?  */
  257.       return (nfiles);                          /* If not, return df=0. */
  258.  
  259.    if (freq_abort(nfiles))                      /* Any reason to abort? */
  260.       return (-2);                              /* If so, return error. */
  261.  
  262.  
  263.    /*--------------------------------------------------------------------*/
  264.    /* Initialization(s)                                                  */
  265.    /*--------------------------------------------------------------------*/
  266.    i = errno = 0;
  267.    sptr = NULL;
  268.  
  269.    strupr (request);
  270.    status_line ("*%s request (%s)", (updreq != 0L) ? "Update" : "File", request);
  271.  
  272.    saved_nfiles = nfiles;
  273.  
  274.    /*--------------------------------------------------------------------*/
  275.    /* Reserved words                                                     */
  276.    /*--------------------------------------------------------------------*/
  277.    if (!strcmp (request, "FILES"))
  278.       {
  279.       if (CurrentFILES)
  280.          strcpy (s, CurrentFILES);
  281.       else
  282.          {
  283.          s[0] = '\0';
  284.          sptr = "No AVAIL list";
  285.          }
  286.       goto avail;
  287.       }
  288.  
  289.    else if (!strcmp (request, "ABOUT"))
  290.       {
  291.       s[0] = '\0';
  292.       goto avail;
  293.       }
  294.  
  295.    prep_match (request, their_wildcard);
  296.  
  297.    /*--------------------------------------------------------------------*/
  298.    /* See if the file is approved for transmission                       */
  299.    /*--------------------------------------------------------------------*/
  300.    approved = fopen (CurrentOKFile, read_ascii);
  301.    if (got_error (OPEN_msg, CurrentOKFile))
  302.       goto err;
  303.  
  304.    while (!feof (approved))
  305.       {
  306.       /* If we were magic, set flag to cause exit if we don't do it again */
  307.       if (magic_state)
  308.          magic_state = 1;                       /* 1 means done if no @ */
  309.  
  310.       s[0] = required_pwd[0] = '\0';
  311.  
  312.       fgets (s, 78, approved);
  313.  
  314.       for (i = 0; s[i]; i++)
  315.          if (s[i] == 0x09)
  316.             s[i] = ' ';
  317.          else if (s[i] < ' ')
  318.             s[i] = '\0';
  319.  
  320.       if (!s[0] || s[0] == ';')
  321.          continue;
  322.  
  323.       /*--------------------------------------------------------------*/
  324.       /* Check for transaction-level password                         */
  325.       /*--------------------------------------------------------------*/
  326.       for (i = 0; s[i]; i++)
  327.          {
  328.          if (s[i] == ' ')
  329.             {
  330.             s[i] = '\0';
  331.             if (s[i + 1] == '!')
  332.                {
  333.                strncpy (required_pwd, s + i + 2, 8);
  334.                if (strlen (required_pwd) > 6)
  335.                   required_pwd[6] = '\0';
  336.  
  337.                after_pwd = s + i + 1;
  338.                while (*after_pwd && (!isspace (*after_pwd)))
  339.                   ++after_pwd;
  340.  
  341.                if (*after_pwd)
  342.                   ++after_pwd;
  343.  
  344.                for (i = 0; required_pwd[i]; i++)
  345.                   if (required_pwd[i] <= ' ')
  346.                      required_pwd[i] = '\0';
  347.  
  348.                break;
  349.                }
  350.             else
  351.                {
  352.                after_pwd = s + i + 1;
  353.                break;
  354.                }
  355.             }
  356.          else if (s[i] < ' ')
  357.             s[i] = '\0';
  358.          }
  359.  
  360.       if (!s[0])
  361.          continue;
  362.  
  363.       if (strchr ("@+$",s[0]) != NULL)
  364.          {
  365.          /* Magic name or function */
  366.          if (stricmp (&s[1], request))
  367.             continue;
  368.  
  369.          /* Name matches, check password */
  370.          if (!(check_password ()))
  371.             {
  372.             failure_reason = 3;      /* Password doesn't match */
  373.             continue;                /* Go on                  */
  374.             }
  375.          
  376.          mfunc = 0;
  377.  
  378.          if (s[0] == '$')
  379.             {
  380.             sprintf (s2, after_pwd, remote_net, remote_node);
  381.             mfunc = 1;
  382.             }
  383.  
  384.          if (s[0] == '+')
  385.             {
  386.             sprintf (s, " %d %d %d", remote_zone, remote_net, remote_node);
  387.             strcpy (s2, s1);
  388.             strcat (s2, s);
  389.             mfunc = 2;
  390.             }
  391.  
  392.          if (mfunc)
  393.             {
  394.             run_prog (s2);
  395.             goto check_HLO;
  396.             }
  397.          
  398.          if (s[0] == '@')
  399.             {         
  400.             strcpy (s, after_pwd);
  401.             magic_state = 2;                    /* Will be reset up above */
  402.             }
  403.          }
  404.  
  405.       /*
  406.        * We're past the magic stuff here. So check for whether this is
  407.        * a new iteration of a magic loop that somehow didn't catch.
  408.        * If not, then check out the filespec we have on this line.
  409.        */
  410.  
  411.       if (magic_state == 1)
  412.          goto check_HLO;
  413.  
  414.       j = 1;
  415.       dh = opendir();   /*PLF Fri  05-05-1989  23:49:02 */
  416.       if (!findfirst(s, _A_NORMAL, dh)) /*PLF Fri  05-05-1989  23:51:03 */
  417.          {
  418.          do
  419.             {
  420.  
  421.             if (!(--j))                         /* If we've looped a bit  */
  422.                {
  423.                if (i = freq_abort (nfiles))     /* See if we want to quit */
  424.                   {
  425.                   if (i == 1)                   /* Yes, why? Carrier?     */
  426.                      goto finished;             /* uh-huh -- get out fast */
  427.                   else
  428.                      {
  429.                      failure_reason = i;        /* Not carrier, get reason*/
  430.                      goto make_RSP;             /* Make a .RSP file       */
  431.                      }
  432.                   }
  433.                j = 10;                          /* No, reset bounds       */
  434.                }
  435.  
  436.             if (!magic_state)                   /* If not "magic",        */
  437.                {
  438.                if (prep_match (dh->name, our_wildcard) < 0)
  439.                   continue;
  440.  
  441.                if (match (our_wildcard, their_wildcard))
  442.                   continue;
  443.  
  444.                /* File names match, check password now */
  445.  
  446.                if (!check_password ())
  447.                   {
  448.                   failure_reason = 3;      /* Password doesn't match */
  449.                   continue;                /* Go on                  */
  450.                   }
  451.                }
  452.  
  453.             /* Good password, get full path with wildcard from OKFILE */
  454.  
  455.             strcpy (s2, s);
  456.  
  457.             /* Subtract the wild card file name, keep path */
  458.  
  459.             for (i = strlen (s2); i; i--)
  460.                if (s2[i] == '\\')
  461.                   {
  462.                   s2[i + 1] = 0;
  463.                   break;
  464.                   }
  465.  
  466.             /* Then add in the exact name found */
  467.  
  468.             strcat (s2, dh->name);
  469.  
  470.             /* Got full filename, now do file update validation */
  471.  
  472.             if (updtype && !stat (s2, &st))
  473.                {
  474.                if ((updtype == '+' && (st.st_atime - timezone <= updreq))
  475.                ||  (updtype == '-' && (st.st_atime - timezone >= updreq)))
  476.                   {
  477.                   failure_reason = 2;      /* No update available    */
  478.                   continue;                /* Go on                  */
  479.                   }
  480.                }                                    
  481.  
  482.             /* Everything is OK, send the file if we can */
  483.  
  484.             if ((*callback) (s2))
  485.                ++nfiles;
  486.             j = 1;                               /* Force abort test */
  487.             }
  488.          while (!findnext(dh));  /*PLF Fri  05-05-1989  23:51:05 */
  489.          }                                       /* if dfind */
  490.  
  491.       else status_line ("!OKFILE ERR `%s'", s);  /* if not dfind */
  492.       closedir(dh); /*PLF Fri  05-05-1989  23:51:55 */
  493.       s[0] = '\0';
  494.       }                                          /* while not eof(approved) */
  495.  
  496.    if (saved_nfiles != nfiles)
  497.       failure_reason = 9;
  498.  
  499. make_RSP:
  500.  
  501.    s[0] = '\0';                                  /* Initialize string     */
  502.    if ((CurrentReqTemplate != NULL) && (dexists(CurrentReqTemplate)))
  503.       {
  504.       Make_Response(s1, failure_reason);         /* Build a response      */
  505.       strcpy(s, s1);                             /* Copy it for xmission  */
  506.       }
  507.  
  508.    if ((!s[0]) && (failure_reason > 3))          /* if no .RSP file,      */
  509.       goto finished;                             /* give it up            */
  510.  
  511.    /*--------------------------------------------------------------------*/
  512.    /* File requested not found, send the system ABOUT file.              */
  513.    /*--------------------------------------------------------------------*/
  514. avail:
  515.  
  516.    if (!s[0])
  517.       {
  518.       if (CurrentAbout)
  519.          strcpy (s, CurrentAbout);
  520.       else
  521.          {
  522.          sptr = "No ABOUT file";
  523.          goto err;
  524.          }
  525.       }
  526.  
  527.    if ((*callback) (s))
  528.          ++nfiles;
  529.  
  530.    goto finished;
  531.  
  532.    /*--------------------------------------------------------------------*/
  533.    /* See if we generated a .QLO file somehow, if so send listed files   */
  534.    /*--------------------------------------------------------------------*/
  535. check_HLO:
  536.  
  537.    i = do_FLOfile ("Q", callback);
  538.    goto finished;
  539.  
  540.    /*--------------------------------------------------------------------*/
  541.    /* Error return                                                       */
  542.    /*--------------------------------------------------------------------*/
  543. err:
  544.    if (sptr)
  545.       status_line ("!%s Request Err: %s", (updreq != 0L) ? "Update" : "File", sptr);
  546.  
  547. finished:
  548.    if (approved)
  549.       fclose (approved);
  550.    errno = 0;
  551.  
  552.    return (nfiles);
  553. }
  554.  
  555. static void run_prog (s)
  556. char *s;
  557. {
  558.    status_line (":Executing '%s'", s);
  559.    if (fullscreen && un_attended)
  560.       {
  561.       scr_printf ("\033[H\033[2J");
  562.       }
  563.    scr_printf ("Executing ");
  564.    scr_printf (s);
  565.    scr_printf ("\r\n");
  566.    vfossil_cursor (1);
  567.    b_spawn (s);
  568.    vfossil_cursor (0);
  569.    if (fullscreen && un_attended)
  570.       {
  571.       scr_printf ("\033[H\033[2J");
  572.       sb_dirty ();
  573.       opening_banner ();
  574.       mailer_banner ();
  575.       }
  576. }
  577.  
  578. static int check_password ()
  579. {
  580.    if (required_pwd[0])
  581.       {
  582.       fancy_str (required_pwd);
  583.       if ((strcmp (required_pwd, their_pwd)) &&
  584.           (strcmp (required_pwd, remote_password)))
  585.          {
  586.          status_line ("!Transaction pwd err: %s %s %s",
  587.                                required_pwd,
  588.                                their_pwd,
  589.                                remote_password
  590.                      );
  591.  
  592.          return (0);
  593.          }
  594.       }
  595.    return (1);
  596. }
  597.  
  598. static int freq_abort (nfiles)
  599. int nfiles;
  600. {
  601.    int w_event;
  602.  
  603.    if (!CARRIER)
  604.       {
  605.       status_line ("!Carrier lost, request(s) aborted");
  606.       return (1);
  607.       }
  608.  
  609.    if (nfiles >= CurrentReqLim)
  610.       {
  611.       status_line ("!File Request limit exceeded");
  612.       return (4);
  613.       }
  614.  
  615.    w_event = what_event ();
  616.    if ((w_event >= 0) && (w_event != cur_event))
  617.       {
  618.       if ((e_ptrs[w_event]->behavior & MAT_NOREQ) ||
  619.          ((w_event != cur_event) && (e_ptrs[w_event]->behavior & MAT_FORCED)))
  620.          {
  621.          status_line ("!Event Overrun - requests aborted");
  622.          return (5);
  623.          }
  624.       }
  625.  
  626.    return (0);
  627. }
  628.